//
//  ViewController.m
//  stringsub
//
//  Created by mac on 2021/8/28.
//

#import "ViewController.h"
#import "YZNeedSubModel.h"

#import "YZMessageModel.h"

#import "MJExtension.h"
#import "MJRefresh.h"

@interface ViewController ()

@property(nonatomic,strong)NSMutableArray * emojArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.emojArray = [NSMutableArray arrayWithArray:[NSArray arrayWithObjects:@"再见", @"笑哭",@"哈哈",nil]];
    NSLog(@"表情字符串数组:%@ \n",self.emojArray);
    NSString * valueInput = @"你说的这是[笑哭]哈哈镜吧你说的额真是令人难受[再见]啊哈[再见]";
//    NSString * valueInput = @"你说的这是哈哈镜吧你说的额真是令人难受";
//    NSString * valueInput = @"[爱哭]你说的这是哈哈镜吧你说的额真是令人难受";
//    NSString * valueInput = @"[笑哭]你说的这是哈哈镜吧你说的额真是令人难受";
//    NSString * valueInput = @"  [笑哭] 你说的这是哈哈 镜吧你说的额真是令人难受";




    NSLog(@"用户输入的全部字符串内容:%@ \n",self.emojArray);

    NSString * value = valueInput;
    NSMutableArray * needReplaceArray = [NSMutableArray arrayWithCapacity:0];
    
    while (![self judgeContain:self.emojArray inputStr:value]) {
        for (int i = 0;i < self.emojArray.count; i ++) {
            NSString * name = self.emojArray[i];
            NSString * nameTotal = [NSString stringWithFormat:@"[%@]",name];
            if ([value containsString:nameTotal]) {
                NSRange range = [value rangeOfString:nameTotal];
                YZNeedSubModel * model = [[YZNeedSubModel alloc] init];
                model.range = range;
                model.emojIndex = i;
                model.emjName = name;
                NSString * repleaseStr;
                for (int o = 0; o < range.length; o ++) {
                    if (repleaseStr.length > 0) {
                        repleaseStr = [NSString stringWithFormat:@"%@*",repleaseStr];
                    }else {
                        repleaseStr = @"*";
                    }
                }
                value = [value stringByReplacingCharactersInRange:range withString:repleaseStr];
                [needReplaceArray addObject:model];
            }
        }
    }

    NSLog(@"用户输入的内容中包含的所有的表情包的数组:%@",needReplaceArray);

    for (int p = 0; p < needReplaceArray.count; p ++) {
        YZNeedSubModel * model = needReplaceArray[p];
        NSLog(@"表情包的名字 = %@,这个表情包对应用户输入的全部字符串的下标值location = %lu",model.emjName,(unsigned long)model.range.location);
    }
    
    //根据表情包的range.location下标对数组进行排序
    NSArray * arrayNeed =  [needReplaceArray sortedArrayUsingComparator:^NSComparisonResult(YZNeedSubModel * obj1, YZNeedSubModel * obj2) {
        if (obj1.range.location < obj2.range.location) {
            return NSOrderedAscending;
        }else {
            return NSOrderedDescending;
        }
    }];
    
    
    NSLog(@"排序后的表情包数组信息");
    for (int i = 0; i < arrayNeed.count; i ++) {
        YZNeedSubModel * model = arrayNeed[i];
        NSLog(@"表情包的名字 = %@,这个表情包对应用户输入的全部字符串的下标值location = %lu",model.emjName,(unsigned long)model.range.location);
    }
    
    
//   表情包和混合输入的文本数据重新组合
    NSMutableArray * arrayEmoj = [NSMutableArray arrayWithArray:arrayNeed];
    NSInteger currentLocation = 0;
    NSInteger totalStringLength = valueInput.length;
    NSInteger currentEmojIndex = 0;
    NSInteger emojTotalCount = arrayEmoj.count;
    
    NSMutableArray * arrayLast = [NSMutableArray arrayWithCapacity:0];
    
    while (currentLocation < totalStringLength) {
        if (currentEmojIndex < emojTotalCount) {
            YZNeedSubModel * model = arrayEmoj[currentEmojIndex];
            if (model.range.location > currentLocation) {
                NSRange range = NSMakeRange(currentLocation, model.range.location - currentLocation);
                currentLocation = model.range.location;
                NSString * textString = [valueInput substringWithRange:range];
                YZMessageModel * modelM = [[YZMessageModel alloc] init];
                modelM.name = textString;
                modelM.emoj = NO;
                modelM.emojIndex = -1;
                [arrayLast addObject:modelM.mj_keyValues];
            }else if(model.range.location == currentLocation){
                YZMessageModel * modelM = [[YZMessageModel alloc] init];
                modelM.name = model.emjName;
                modelM.emoj = YES;
                modelM.emojIndex = model.emojIndex;
                [arrayLast addObject:modelM.mj_keyValues];
                currentLocation += model.range.length;
                currentEmojIndex ++;
            }
        }else {
            NSRange range = NSMakeRange(currentLocation, totalStringLength - currentLocation);
            NSString * textString = [valueInput substringWithRange:range];
            YZMessageModel * modelM = [[YZMessageModel alloc] init];
            modelM.name = textString;
            modelM.emoj = NO;
            modelM.emojIndex = -1;
            [arrayLast addObject:modelM.mj_keyValues];
            currentLocation += range.length;
        }
    }
    
    NSLog(@"arrayLast info = %@",arrayLast.mj_JSONString);
    
}


-(BOOL)judgeContain:(NSArray *)emojArray inputStr:(NSString *)inputStr {
    NSLog(@"%@ %@",emojArray,inputStr);
    for (int i = 0; i < emojArray.count;i ++) {
        NSString * name = emojArray[i];
        NSString * totalName = [NSString stringWithFormat:@"[%@]",name];
        if ([inputStr containsString:totalName]) {
            return NO;
        }
    }
    return YES;
}


@end
